package util;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import com.example.administrator.text.R;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/7/8.
 *获取验证码
 */
public class TimeCountUtil{
    private  Context mContext;
    private String mString = "秒后可重新获取";
    private TextView mTV;//按钮
    private Timer mTimer = new Timer();//倒计时
    private int mIllisInFuture;//总时间
    private int mShui;//0或1
    private int mTpye; // 0 代表登录的样式  1 代表重置

    /**
     * @param illisInFuture 总时间
     * @param tv 对应的控件
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public TimeCountUtil(Context context, int illisInFuture, final TextView tv,int shui,int type) {
        this.mContext = context;
        this.mIllisInFuture = illisInFuture;
        this.mTV = tv;
        this.mShui = shui;
        this.mTpye = type;
        final Handler handler = new Handler(){
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        tv.setText(""+mIllisInFuture+mString);
                        if(mIllisInFuture < 1){
                            mTimer.cancel();
                            tv.setClickable(true);
                            tv.setText("重新获取");
                            if(mTpye == 0){
                                tv.setTextColor(mContext.getResources().getColor(R.color.we_wire));
                                tv.setBackground(mContext.getResources().getDrawable(R.color.Land_true));
                            }
                        }
                }
            }
        };

        TimerTask task = new TimerTask() {
            @Override public void run() {
                mIllisInFuture--;
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        };

        if(mShui == 0){
            tv.setText("" +mIllisInFuture+mString);
            tv.setClickable(false);
            if(mTpye == 0){
                tv.setBackground(mContext.getResources().getDrawable(R.color.Land_false));
            }
            mTimer.schedule(task, 1000, 1000);       //循环执行
        }else {
            tv.setText("重新获取");
        }
    }
}
